conn = DBUtil.getConnection();
            conn.setAutoCommit(true); // 设置为自动提交模式，确保每条SQL立即执行
            String sql = "UPDATE 关于用户 SET pword = ?, water = ?, elec = ? WHERE User = ?";
            pstmt = conn.prepareStatement(sql);

            for (int i = 0; i < tableModel.getRowCount(); ++i) {
                // 检查表格列数是否足够
                if (tableModel.getColumnCount() < 9) {
                    JOptionPane.showMessageDialog(this, "表格列数不足，请检查数据源", "错误", JOptionPane.ERROR_MESSAGE);
                    return;
                }

                String username = (String) tableModel.getValueAt(i, 0);
                String password = (String) tableModel.getValueAt(i, 1);
                String wuyefei  = (String) tableModel.getValueAt(i, 5);
                // 类型检查与转换
                Object waterObj = tableModel.getValueAt(i, 2);
                Object electricityObj = tableModel.getValueAt(i, 3);
                Object WupayStateObj = tableModel.getValueAt(i, 6);
                Object ElecPayStateObj = tableModel.getValueAt(i, 7);
                Object WaterPayStateObj = tableModel.getValueAt(i, 8);

                double water;
                try {
                    water = waterObj instanceof Double ? (Double) waterObj : Double.parseDouble(waterObj.toString());
                } catch (NumberFormatException ex) {
                    JOptionPane.showMessageDialog(this, "第 " + (i + 1) + " 行用水量格式错误，请输入数字", "错误", JOptionPane.ERROR_MESSAGE);
                    return;
                }

                double electricity;
                try {
                    electricity = electricityObj instanceof Double ? (Double) electricityObj : Double.parseDouble(electricityObj.toString());
                } catch (NumberFormatException ex) {
                    JOptionPane.showMessageDialog(this, "第 " + (i + 1) + " 行用电量格式错误，请输入数字", "错误", JOptionPane.ERROR_MESSAGE);
                    return;
                }
            }
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(this, "数据库操作失败: " + ex.getMessage(), "错误", JOptionPane.ERROR_MESSAGE);
        } finally {
            try {
                if (pstmt != null) pstmt.close();
                if (conn != null) conn.close();
            } catch (SQLException ex) {
                JOptionPane.showMessageDialog(this, "关闭数据库连接失败: " + ex.getMessage(), "错误", JOptionPane.ERROR_MESSAGE);
            }
        }
    }

    private void setParameterValue(int columnIndex, Object value) throws SQLException {
        switch (columnIndex) {
            case 1: // 处理密码字段
                if (!(value instanceof String)) {
                    throw new IllegalArgumentException("密码字段必须为字符串类型");
                }
                pstmt.setString(1, (String) value);
                break;
            case 2: // 处理水费字段
                try {
                    double water = value instanceof Double ? (Double) value : Double.parseDouble(value.toString());
                    pstmt.setDouble(2, water);
                } catch (NumberFormatException ex) {
                    throw new SQLException("请输入有效的数字");
                }
                break;
            case 3: // 新增：处理物业费状态
            case 4: // 新增：处理电费状态
            case 5: // 新增：处理水费状态
                if (!(value instanceof String)) {
                    throw new IllegalArgumentException("状态字段必须为字符串类型");
                }
                // 修正：使用正确的参数索引
                pstmt.setString(columnIndex, (String) value);
                break;
            default:
                throw new IllegalArgumentException("无效的选择: 列索引超出范围或类型不匹配");
        }
    }
}
